package com.ds.common.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * <!--xstream-->
 * <dependency>
 * <groupId>com.thoughtworks.xstream</groupId>
 * <artifactId>xstream</artifactId>
 * <version>1.4.20</version>
 * </dependency>
 *
 * @Description: 将Java对象序列化成XML格式，将XML反序列化还原为Java对象
 */
public class XmlUtils extends XStream {
    /**
     * 将Java对象序列化成XML格式
     *
     * @param
     * @return
     * @throws IOException
     */
    public static <T> String beanToXml(T t) {

        XStream xstream = new XStream(new StaxDriver());
        XStream.setupDefaultSecurity(xstream);
        xstream.processAnnotations(t.getClass());
        xstream.allowTypeHierarchy(t.getClass());
        String personXml = xstream.toXML(t);
        return personXml;
    }

    /**
     * 将XML反序列化还原为Java对象
     * java对象类型可配置
     *
     * @param xmlStr
     * @return
     */
    public static <T> T xmlToBean(String xmlStr, Class<T> cls) {
        XStream xstream = new XStream(new StaxDriver());
        XStream.setupDefaultSecurity(xstream);
        //xstream使用注解转换
        xstream.processAnnotations(cls);
        xstream.allowTypeHierarchy(cls);
        //xstream.autodetectAnnotations(true);
        Object obj = xstream.fromXML(xmlStr);
        return (T) obj;
    }


    //public static void main(String[] args) {
    //    String xml = """
    //            <xml>
    //              <ToUserName>wwdd77bb88473c3134</ToUserName>
    //              <FromUserName>dorom</FromUserName>
    //              <CreateTime>1699922076</CreateTime>
    //              <MsgType>text</MsgType>
    //              <Content>11111</Content>
    //              <MsgId>7301109722474487597</MsgId>
    //              <AgentID>1000051</AgentID>
    //            </xml>
    //            """;
    //
    //    WxMsg wxMsg = xmlToBean(xml, WxMsg.class);
    //    System.out.println("wxMsg = " + JSONObject.toJSONString(wxMsg));
    //
    //    wxMsg.setList(new ArrayList<>(Arrays.asList(new User("张三", 1), new User("李四", 2))));
    //    String s = beanToXml(wxMsg);
    //    System.out.println(s);
    //}


    @Data
    @XStreamAlias("xml")
    static class WxMsg implements Serializable {
        private static final long serialVersionUID = 1L;

        @XStreamAlias("ToUserName")
        private String toUserName;

        @XStreamAlias("FromUserName")
        private String fromUserName;

        @XStreamAlias("CreateTime")
        private String createTime;

        @XStreamAlias("MsgType")
        private String msgType;

        @XStreamAlias("Content")
        private String content;

        @XStreamAlias("MsgId")
        private String msgId;

        @XStreamAlias("AgentID")
        private String agentId;


        @XStreamAlias("list")
        //@XStreamOmitField  // 忽略字段
        @XStreamImplicit // 不序列化标签
        private List<User> list;
    }

    @Data
    @AllArgsConstructor
    @XStreamAlias("user")
    static class User implements Serializable {
        private static final long serialVersionUID = 1L;
        @XStreamAlias("username")
        private String username;
        @XStreamAlias("id")
        private Integer id;
    }
}
 